﻿using AutoMapper;
using MediatR;
using SmartProduction.Domain.客户管理;
using SmartProduction.ErrorCode;
using SmartProduction.Infarstructrue.Interface;
using SmartProduction.Read.API.Application.Commands.ResourceInfoCommand;
using SmartProduction.Read.API.Dtos;

namespace SmartProduction.Read.API.Application.CommandHandlers.ResourceInfoCommandHnadler
{
    /// <summary>
    /// 资源列表查询命令处理
    /// </summary>
    public class ResourceInfoQueryCommandHandler : IRequestHandler<ResourceInfoQueryCommand, APIResult<APIPaing<ResourceDTO>>>
    {
        /// <summary>
        /// 资源信息仓储
        /// </summary>
        private readonly IBaseRepository<ResourceInfo> infoRepository;

        /// <summary>
        /// 资源单仓储
        /// </summary>
        private readonly IBaseRepository<ResourceUnit> unitRepository;

        /// <summary>
        /// 映射
        /// </summary>
        private readonly IMapper mapper;

        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="InfoRepository">资源信息仓储</param>
        /// <param name="unitRepository">资源单仓储</param>
        /// <param name="mapper">映射</param>
        public ResourceInfoQueryCommandHandler(IBaseRepository<ResourceInfo> InfoRepository,IBaseRepository<ResourceUnit> unitRepository,IMapper mapper)
        {
            infoRepository = InfoRepository;
            this.unitRepository = unitRepository;
            this.mapper = mapper;
        }

        /// <summary>
        /// 处理
        /// </summary>
        /// <param name="request">请求</param>
        /// <param name="cancellationToken">取消</param>
        /// <returns>返回任务</returns>
        public Task<APIResult<APIPaing<ResourceDTO>>> Handle(ResourceInfoQueryCommand request, CancellationToken cancellationToken)
        {
            try
            {
                APIResult<APIPaing<ResourceDTO>> result = new APIResult<APIPaing<ResourceDTO>>();
                result.Code = APIEnums.查询成功;
                result.Msg = APIEnums.查询成功.ToString();
                var linq = from a in unitRepository.GetAll().Result
                           join b in infoRepository.GetAll().Result on a.ResourceId equals b.ResourceId
                           select new ResourceDTO
                           {
                               ResourceId = a.ResourceId,
                               ResourceDescribe = b.ResourceDescribe,
                               ResourceType = b.ResourceType,
                               ResourceInfoId = b.ResourceInfoId,
                               ResourceInfoName = b.ResourceInfoName,
                               ResourceName = a.ResourceName,
                               ResourceNumer = b.ResourceNumer,
                               ResourceStatus = b.ResourceStatus,
                               ChargWay = b.ChargWay,
                               CountCost = b.CountCost,
                               StandardType = b.StandardType,
                               Costabsorb = b.Costabsorb,
                               TypeExpenceId = b.TypeExpenceId,
                               UnitCost = b.UnitCost,
                           };
                var count = linq.Count();
                var page = (int)Math.Ceiling(count * 1.0 / request.PageSize);
                var models = linq.OrderByDescending(x => x.ResourceInfoId).Skip((request.PageIndex - 1) * request.PageSize).Take(request.PageSize);
                APIPaing<ResourceDTO> paging = new APIPaing<ResourceDTO>();
                paging.PageCount = page;
                paging.TotalCount = count;
                paging.Items = mapper.Map<List<ResourceDTO>>(models);
                result.data = paging;
                return Task.FromResult(result);
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
